home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Stylepad / Stylepad.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  18.7 KB  |  530 lines

  1. /*
  2.  * @(#)Stylepad.java    1.8 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.net.URL;
  19. import java.util.Locale;
  20. import java.util.MissingResourceException;
  21. import java.util.ResourceBundle;
  22. import javax.swing.text.*;
  23. import javax.swing.*;
  24.  
  25. import java.io.*;
  26.  
  27. /**
  28.  * Sample application using JTextPane.
  29.  *
  30.  * @author Timothy Prinzing
  31.  * @version 1.8 08/26/98
  32.  */
  33. public class Stylepad extends Notepad {
  34.  
  35.     private static ResourceBundle resources;
  36.  
  37.     static {
  38.         try {
  39.             resources = ResourceBundle.getBundle("resources.Stylepad", 
  40.                                                  Locale.getDefault());
  41.         } catch (MissingResourceException mre) {
  42.             System.err.println("Stylepad.properties not found");
  43.             System.exit(0);
  44.         }
  45.     }
  46.  
  47.     public Stylepad() {
  48.     super();
  49.     }
  50.     
  51.     public static void main(String[] args) {
  52.         String vers = System.getProperty("java.version");
  53.         if (vers.compareTo("1.1.2") < 0) {
  54.             System.out.println("!!!WARNING: Swing must be run with a " +
  55.                                "1.1.2 or higher version VM!!!");
  56.         }
  57.         JFrame frame = new JFrame();
  58.         frame.setTitle(resources.getString("Title"));
  59.     frame.setBackground(Color.lightGray);
  60.     frame.getContentPane().setLayout(new BorderLayout());
  61.     frame.getContentPane().add("Center", new Stylepad());
  62.     frame.addWindowListener(new AppCloser());
  63.     frame.pack();
  64.     frame.setSize(600, 480);
  65.         frame.show();
  66.     }
  67.  
  68.     /**
  69.      * Fetch the list of actions supported by this
  70.      * editor.  It is implemented to return the list
  71.      * of actions supported by the superclass
  72.      * augmented with the actions defined locally.
  73.      */
  74.     public Action[] getActions() {
  75.         Action[] defaultActions = {
  76.             new NewAction(),
  77.             new OpenAction(),
  78.             new SaveAction(),
  79.             new ChangeKeymapAction("English"),
  80.             new ChangeKeymapAction("Hebrew"),
  81.             new ChangeKeymapAction("Arabic")
  82.         };
  83.         Action[] a = TextAction.augmentList(super.getActions(), defaultActions);
  84.         a = TextAction.augmentList(a, hebrewActions);
  85.         a = TextAction.augmentList(a, arabicActions);
  86.     return a;
  87.     }
  88.  
  89.     /**
  90.      * Try and resolve the resource name in the local
  91.      * resource file, and if not found fall back to
  92.      * the superclass resource file.
  93.      */
  94.     protected String getResourceString(String nm) {
  95.     String str;
  96.     try {
  97.         str = this.resources.getString(nm);
  98.     } catch (MissingResourceException mre) {
  99.         str = super.getResourceString(nm);
  100.     }
  101.     return str;
  102.     }
  103.  
  104.     /**
  105.      * Create an editor to represent the given document.  
  106.      */
  107.     protected JTextComponent createEditor() {
  108.     StyleContext sc = new StyleContext();
  109.     DefaultStyledDocument doc = new DefaultStyledDocument(sc);
  110.     initDocument(doc, sc);
  111.         JTextPane p = new JTextPane(doc);
  112.         
  113.         Keymap parent = p.getKeymap();
  114.         Keymap english = p.addKeymap("English", parent);
  115.         Keymap hebrew = p.addKeymap("Hebrew", parent);
  116.         JTextComponent.loadKeymap( hebrew, hebrewBindings, hebrewActions );
  117.         Keymap arabic = p.addKeymap("Arabic", parent);
  118.         JTextComponent.loadKeymap( arabic, arabicBindings, arabicActions );
  119.         p.setKeymap(english);
  120.  
  121.         //p.getCaret().setBlinkRate(0);
  122.         
  123.         return p;
  124.     }
  125.  
  126.     /**
  127.      * Create a menu for the app.  This is redefined to trap 
  128.      * a couple of special entries for now.
  129.      */
  130.     protected JMenu createMenu(String key) {
  131.     if (key.equals("color")) {
  132.         return createColorMenu();
  133.     } 
  134.     return super.createMenu(key);
  135.     }
  136.  
  137.  
  138.     // this will soon be replaced
  139.     JMenu createColorMenu() {
  140.     ActionListener a;
  141.     JMenuItem mi;
  142.     JMenu menu = new JMenu(getResourceString("color" + labelSuffix));
  143.     mi = new JMenuItem(resources.getString("Red"));
  144.     mi.setHorizontalTextPosition(JButton.RIGHT);
  145.     mi.setIcon(new ColoredSquare(Color.red));
  146.     a = new StyledEditorKit.ForegroundAction("set-foreground-red", Color.red);
  147.     //a = new ColorAction(se, Color.red);
  148.     mi.addActionListener(a);
  149.     menu.add(mi);
  150.     mi = new JMenuItem(resources.getString("Green"));
  151.     mi.setHorizontalTextPosition(JButton.RIGHT);
  152.     mi.setIcon(new ColoredSquare(Color.green));
  153.     a = new StyledEditorKit.ForegroundAction("set-foreground-green", Color.green);
  154.     //a = new ColorAction(se, Color.green);
  155.     mi.addActionListener(a);
  156.     menu.add(mi);
  157.     mi = new JMenuItem(resources.getString("Blue"));
  158.     mi.setHorizontalTextPosition(JButton.RIGHT);
  159.     mi.setIcon(new ColoredSquare(Color.blue));
  160.     a = new StyledEditorKit.ForegroundAction("set-foreground-blue", Color.blue);
  161.     //a = new ColorAction(se, Color.blue);
  162.     mi.addActionListener(a);
  163.     menu.add(mi);
  164.  
  165.     return menu;
  166.     }
  167.  
  168.     void initDocument(DefaultStyledDocument doc, StyleContext sc) {
  169.     Wonderland w = new Wonderland(doc, sc);
  170.     //HelloWorld h = new HelloWorld(doc, sc);
  171.     Icon alice = new ImageIcon(resources.getString("aliceGif"));
  172.     w.loadDocument();
  173.     }
  174.  
  175.     JComboBox createFamilyChoices() {
  176.         JComboBox b = new JComboBox();
  177.     String[] fonts = getToolkit().getFontList();
  178.     for (int i = 0; i < fonts.length; i++) {
  179.         b.addItem(fonts[i]);
  180.     }
  181.     return b;
  182.     }
  183.  
  184.     /**
  185.      * Trys to read a file which is assumed to be a 
  186.      * serialization of a document.
  187.      */
  188.     class OpenAction extends AbstractAction {
  189.  
  190.     OpenAction() {
  191.         super(openAction);
  192.     }
  193.  
  194.         public void actionPerformed(ActionEvent e) {
  195.         Frame frame = getFrame();
  196.         if (fileDialog == null) {
  197.         fileDialog = new FileDialog(frame);
  198.         }
  199.         fileDialog.setMode(FileDialog.LOAD);
  200.         fileDialog.show();
  201.         
  202.         String file = fileDialog.getFile();
  203.         if (file == null) {
  204.         return;
  205.         }
  206.         String directory = fileDialog.getDirectory();
  207.         File f = new File(directory, file);
  208.         if (f.exists()) {
  209.         try {
  210.             FileInputStream fin = new FileInputStream(f);
  211.             ObjectInputStream istrm = new ObjectInputStream(fin);
  212.             Document doc = (Document) istrm.readObject();
  213.             getEditor().setDocument(doc);
  214.             frame.setTitle(file);
  215.             validate();
  216.         } catch (IOException io) {
  217.             // should put in status panel
  218.             System.err.println("IOException: " + io.getMessage());
  219.         } catch (ClassNotFoundException cnf) {
  220.             // should put in status panel
  221.             System.err.println("Class not found: " + cnf.getMessage());
  222.         }
  223.         } else {
  224.         // should put in status panel
  225.         System.err.println("No such file: " + f);
  226.         }
  227.     }
  228.     }
  229.  
  230.     /**
  231.      * Trys to write the document as a serialization.
  232.      */
  233.     class SaveAction extends AbstractAction {
  234.  
  235.     SaveAction() {
  236.         super(saveAction);
  237.     }
  238.  
  239.         public void actionPerformed(ActionEvent e) {
  240.         Frame frame = getFrame();
  241.         if (fileDialog == null) {
  242.         fileDialog = new FileDialog(frame);
  243.         }
  244.         fileDialog.setMode(FileDialog.SAVE);
  245.         fileDialog.show();
  246.         String file = fileDialog.getFile();
  247.         if (file == null) {
  248.         return;
  249.         }
  250.         String directory = fileDialog.getDirectory();
  251.         File f = new File(directory, file);
  252.         try {
  253.         FileOutputStream fstrm = new FileOutputStream(f);
  254.         ObjectOutput ostrm = new ObjectOutputStream(fstrm);
  255.         ostrm.writeObject(getEditor().getDocument());
  256.         ostrm.flush();
  257.         } catch (IOException io) {
  258.         // should put in status panel
  259.         System.err.println("IOException: " + io.getMessage());
  260.         }
  261.     }
  262.     }
  263.  
  264.     /**
  265.      * Creates an empty document.
  266.      */
  267.     class NewAction extends AbstractAction {
  268.  
  269.     NewAction() {
  270.         super(newAction);
  271.     }
  272.  
  273.         public void actionPerformed(ActionEvent e) {
  274.         if(getEditor().getDocument() != null)
  275.         getEditor().getDocument().removeUndoableEditListener
  276.                     (undoHandler);
  277.         getEditor().setDocument(new DefaultStyledDocument());
  278.         getEditor().getDocument().addUndoableEditListener(undoHandler);
  279.         validate();
  280.     }
  281.     }
  282.  
  283.     class ColoredSquare implements Icon {
  284.     Color color;
  285.     public ColoredSquare(Color c) {
  286.         this.color = c;
  287.     }
  288.  
  289.     public void paintIcon(Component c, Graphics g, int x, int y) {
  290.         Color oldColor = g.getColor();
  291.         g.setColor(color);
  292.         g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
  293.         g.setColor(oldColor);
  294.     }
  295.     public int getIconWidth() { return 12; }
  296.     public int getIconHeight() { return 12; }
  297.  
  298.     }
  299.  
  300.     /**
  301.      * Change the keyboard mapping.
  302.      * @see DefaultEditorKit#insertBreakAction
  303.      * @see DefaultEditorKit#getActions
  304.      */
  305.     static class ChangeKeymapAction extends TextAction {
  306.  
  307.         /**
  308.          * Creates this object with the appropriate identifier.
  309.          */
  310.         public ChangeKeymapAction( String keymapName ) {
  311.             super("change-keymap-" + keymapName);
  312.             this.keymapName = keymapName;
  313.         }
  314.  
  315.         /**
  316.          * The operation to perform when this action is triggered.
  317.          *
  318.          * @param e the action event
  319.          */
  320.         public void actionPerformed(ActionEvent e) {
  321.             JTextComponent target = getTextComponent(e);
  322.             if (target != null) {
  323.                 Keymap map = target.getKeymap( keymapName );
  324.                 if( map != null )
  325.                     target.setKeymap( map );
  326.             }
  327.         }
  328.         
  329.         private String keymapName;
  330.     }
  331.  
  332.     /**
  333.      * Places a predetermined character into the content.
  334.      * @see DefaultEditorKit#insertBreakAction
  335.      * @see DefaultEditorKit#getActions
  336.      */
  337.     static class InsertMeAction extends TextAction {
  338.  
  339.         /**
  340.          * Creates this object with the appropriate identifier.
  341.          */
  342.         public InsertMeAction( String s ) {
  343.             super("insert-me " + s);
  344.             this.s = s;
  345.         }
  346.  
  347.         /**
  348.          * The operation to perform when this action is triggered.
  349.          *
  350.          * @param e the action event
  351.          */
  352.         public void actionPerformed(ActionEvent e) {
  353.             JTextComponent target = getTextComponent(e);
  354.             if (target != null) {
  355.                 target.replaceSelection(s);
  356.             }
  357.         }
  358.         
  359.         private String s;
  360.     }
  361.  
  362.     static final JTextComponent.KeyBinding[] hebrewBindings = {
  363.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('a'),
  364.                                       "insert-me \u05E9"),
  365.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('b'),
  366.                                       "insert-me \u05E0"),
  367.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('c'),
  368.                                       "insert-me \u05D1"),
  369.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('d'),
  370.                                       "insert-me \u05D2"),
  371.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('e'),
  372.                                       "insert-me \u05E7"),
  373.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('f'),
  374.                                       "insert-me \u05DB"),
  375.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('g'),
  376.                                       "insert-me \u05E2"),
  377.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('h'),
  378.                                       "insert-me \u05D9"),
  379.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('i'),
  380.                                       "insert-me \u05DF"),
  381.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('j'),
  382.                                       "insert-me \u05D7"),
  383.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('k'),
  384.                                       "insert-me \u05DC"),
  385.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('l'),
  386.                                       "insert-me \u05DA"),
  387.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('m'),
  388.                                       "insert-me \u05E6"),
  389.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('n'),
  390.                                       "insert-me \u05DE"),
  391.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('o'),
  392.                                       "insert-me \u05DD"),
  393.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('p'),
  394.                                       "insert-me \u05E4"),
  395.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('q'),
  396.                                       "insert-me /"),
  397.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('r'),
  398.                                       "insert-me \u05E8"),
  399.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('s'),
  400.                                       "insert-me \u05D3"),
  401.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('t'),
  402.                                       "insert-me \u05D0"),
  403.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('u'),
  404.                                       "insert-me \u05D5"),
  405.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('v'),
  406.                                       "insert-me \u05D4"),
  407.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('w'),
  408.                                       "insert-me '"),
  409.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('x'),
  410.                                       "insert-me \u05E1"),
  411.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('y'),
  412.                                       "insert-me \u05D8"),
  413.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('z'),
  414.                                       "insert-me \u05D6"),
  415.     };
  416.  
  417.     static final Action[] hebrewActions = {
  418.         new InsertMeAction("\u05D6"),
  419.         new InsertMeAction("\u05D8"),
  420.         new InsertMeAction("\u05E1"),
  421.         new InsertMeAction("'"),
  422.         new InsertMeAction("\u05D4"),
  423.         new InsertMeAction("\u05D5"),
  424.         new InsertMeAction("\u05D0"),
  425.         new InsertMeAction("\u05D3"),
  426.         new InsertMeAction("\u05E8"),
  427.         new InsertMeAction("/"),
  428.         new InsertMeAction("\u05E4"),
  429.         new InsertMeAction("\u05DD"),
  430.         new InsertMeAction("\u05DE"),
  431.         new InsertMeAction("\u05E6"),
  432.         new InsertMeAction("\u05DA"),
  433.         new InsertMeAction("\u05DC"),
  434.         new InsertMeAction("\u05D7"),
  435.         new InsertMeAction("\u05DF"),
  436.         new InsertMeAction("\u05D9"),
  437.         new InsertMeAction("\u05E2"),
  438.         new InsertMeAction("\u05DB"),
  439.         new InsertMeAction("\u05E7"),
  440.         new InsertMeAction("\u05D2"),
  441.         new InsertMeAction("\u05D1"),
  442.         new InsertMeAction("\u05E0"),
  443.         new InsertMeAction("\u05E9")
  444.     };
  445.     
  446.     static final JTextComponent.KeyBinding[] arabicBindings = {
  447.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('a'),
  448.                                       "insert-me \u0634"),
  449.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('b'),
  450.                                       "insert-me \u0644\u0627"),
  451.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('c'),
  452.                                       "insert-me \u0624"),
  453.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('d'),
  454.                                       "insert-me \u064A"),
  455.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('e'),
  456.                                       "insert-me \u062B"),
  457.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('f'),
  458.                                       "insert-me \u0628"),
  459.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('g'),
  460.                                       "insert-me \u0644"),
  461.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('h'),
  462.                                       "insert-me \u0627"),
  463.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('i'),
  464.                                       "insert-me \u0647"),
  465.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('j'),
  466.                                       "insert-me \u062A"),
  467.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('k'),
  468.                                       "insert-me \u0646"),
  469.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('l'),
  470.                                       "insert-me \u0645"),
  471.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('m'),
  472.                                       "insert-me \u0629"),
  473.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('n'),
  474.                                       "insert-me \u0649"),
  475.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('o'),
  476.                                       "insert-me \u062E"),
  477.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('p'),
  478.                                       "insert-me \u062D"),
  479.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('q'),
  480.                                       "insert-me \u0636"),
  481.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('r'),
  482.                                       "insert-me \u0642"),
  483.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('s'),
  484.                                       "insert-me \u0633"),
  485.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('t'),
  486.                                       "insert-me \u0641"),
  487.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('u'),
  488.                                       "insert-me \u0639"),
  489.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('v'),
  490.                                       "insert-me \u0631"),
  491.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('w'),
  492.                                       "insert-me \u0635"),
  493.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('x'),
  494.                                       "insert-me \u0621"),
  495.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('y'),
  496.                                       "insert-me \u063A"),
  497.         new JTextComponent.KeyBinding(KeyStroke.getKeyStroke('z'),
  498.                                       "insert-me \u0626")
  499.     };
  500.  
  501.     static final Action[] arabicActions = {
  502.         new InsertMeAction("\u0634"),
  503.         new InsertMeAction("\u0644\u0627"),
  504.         new InsertMeAction("\u0624"),
  505.         new InsertMeAction("\u064A"),
  506.         new InsertMeAction("\u062B"),
  507.         new InsertMeAction("\u0628"),
  508.         new InsertMeAction("\u0644"),
  509.         new InsertMeAction("\u0627"),
  510.         new InsertMeAction("\u0647"),
  511.         new InsertMeAction("\u062A"),
  512.         new InsertMeAction("\u0646"),
  513.         new InsertMeAction("\u0645"),
  514.         new InsertMeAction("\u0629"),
  515.         new InsertMeAction("\u0649"),
  516.         new InsertMeAction("\u062E"),
  517.         new InsertMeAction("\u062D"),
  518.         new InsertMeAction("\u0636"),
  519.         new InsertMeAction("\u0642"),
  520.         new InsertMeAction("\u0633"),
  521.         new InsertMeAction("\u0641"),
  522.         new InsertMeAction("\u0639"),
  523.         new InsertMeAction("\u0631"),
  524.         new InsertMeAction("\u0635"),
  525.         new InsertMeAction("\u0621"),
  526.         new InsertMeAction("\u063A"),
  527.         new InsertMeAction("\u0626")
  528.     };
  529. }
  530.